//
//  ST_DesignDAWebController.swift
//  btvideo
//
//  Created by Jason on 2022/3/4.
//  Copyright © 2022 JarvanZhang. All rights reserved.
//

import UIKit
import WebKit

class ST_DesignDAWebController: UIViewController,ST_VMControllerLightNavBarProduce {
    
    private let link:URL?
    init(title:String,link:URL?){
        self.link = link
        super.init(nibName: nil, bundle: nil)
        self.title = title
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        defer {
            layout_navigation_bar()
        }
        
        view.backgroundColor = .white
        webView.backgroundColor = .clear
        webView.isOpaque = false
        webView.navigationDelegate = self
        
        self.view.addSubview(webView)
        let line = UIView()
        line.backgroundColor = UIColor.init(hex: 0x999999, transparency: 0.3)
        self.view.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(statusBarHeight+44)
            make.left.right.equalTo(self.view)
            make.height.equalTo(1)
        }
        webView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(line.snp.bottom)
        }
        
        
        if let url = self.link{
            webView.load(URLRequest(url: url))
        }
        
        
    }
    
    //MARK: - Properties
    
    lazy var tain_navigation_bar:ST_TIANLightNavigationBar = {
        let c = ST_TIANLightNavigationBar()
        c.backgroundColor = .clear
        c.title_nav_btn.setTitle(self.title, for: .normal)
        return c
    }()
    
    private let webView = WKWebView()
    

}


extension ST_DesignDAWebController:WKNavigationDelegate{
    
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
            if navigationAction.navigationType == WKNavigationType.linkActivated {
                
                print("link request =",navigationAction.request)
                
                if let judge_u_r_l = navigationAction.request.url{
                    UIApplication.shared.open(judge_u_r_l)
                }
                
                decisionHandler(WKNavigationActionPolicy.cancel)
                return
            }
            print("no link")
            decisionHandler(WKNavigationActionPolicy.allow)
     }
}
